package com.teacher.pc.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.pojo.dto.flow.ApproveFlowDto;
import com.teacher.pojo.dto.flow.MyApplyGetDto;
import com.teacher.pojo.dto.flow.MyApproveGetDto;
import com.teacher.pojo.entity.BaseEntity;
import com.teacher.pojo.entity.Flow;
import com.teacher.pojo.enums.FlowApproveType;
import com.teacher.service.IFlowService;
import com.teacher.service.impl.RedissonDistributedLocker;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 流程总表 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-03
 */
@RestController
@RequestMapping("/pc/flow")
public class FlowController {

    @Autowired
    private IFlowService iFlowService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private RedissonDistributedLocker locker;

    private static final String FLOW_APPROVE_LOCK = "FLOW_APPROVE_LOCK";

    /**
     * 删除流程
     *
     * @param flowId
     * @return
     */
    @DeleteMapping("/{flowId}")
    @ApiOperation("删除流程")
    public CommonResult<String> del(@PathVariable("flowId") Long flowId) {
        return iFlowService.del(flowId);
    }

    /**
     * 审批流程
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("审批流程")
    public CommonResult<String> approve(@Validated @RequestBody ApproveFlowDto dto) {
        RLock rLock = locker.lock(FLOW_APPROVE_LOCK + dto.getId(), 5000);
        CommonResult<String> commonResult = iFlowService.approve(dto);
        rLock.unlock();
        return commonResult;
    }

    /**
     * 查询我的申请列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/myList")
    @ApiOperation("查询我的申请列表")
    public CommonResult<IPage<Flow>> getMyApply(MyApplyGetDto dto) {
        Date endTime = null;
        if (dto.getCreatTime() != null) {
            endTime = DateUtil.endOfDay(dto.getCreatTime());
        }
        return CommonResult.success(iFlowService.page(new Page<>(dto.getCurrentPage(), dto.getPageSize()), new QueryWrapper<Flow>().lambda()
                .eq(Flow::getApplyUsername, jwtTokenUtil.getUsernameByRequest(httpServletRequest))
                .eq(dto.getStatus() != null, Flow::getStatus, dto.getStatus())
                .like(!StringUtils.isEmpty(dto.getTitle()), Flow::getTitle, dto.getTitle())
                .eq(!StringUtils.isEmpty(dto.getType()), Flow::getType, dto.getType())
                .between(dto.getCreatTime() != null, BaseEntity::getCreateTime, dto.getCreatTime(), endTime)
                .orderByDesc(BaseEntity::getCreateTime)));
    }


    /**
     * 查询我的待审批列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/myApproveList")
    @ApiOperation("查询我的待审批列表 10->德育素质 20->学生干部任职 30->好人好事 40->公益活动")
    public CommonResult<IPage<Flow>> getMyApproveList(MyApproveGetDto dto) {
        return CommonResult.success(iFlowService.page(new Page<>(dto.getCurrentPage(), dto.getPageSize()), new QueryWrapper<Flow>().lambda()
                .eq(Flow::getCurrentApproveUsername, jwtTokenUtil.getUsernameByRequest(httpServletRequest))
                .eq(!StringUtils.isEmpty(dto.getType()), Flow::getType, dto.getType())
                .eq(Flow::getStatus, FlowApproveType.WAIT.getCode())
                .like(!StringUtils.isEmpty(dto.getTitle()), Flow::getTitle, dto.getTitle())
                .orderByDesc(BaseEntity::getCreateTime)));
    }

    /**
     * 查询我的已办
     *
     * @param dto
     * @return
     */
    @GetMapping("/hasApprove")
    @ApiOperation("查询已办")
    public CommonResult<IPage<Flow>> getHasApprove(MyApproveGetDto dto) {
        return iFlowService.getHasApprove(dto);
    }

}

